package com.rongke.web;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.rongke.commons.JsonResp;
import com.rongke.model.Classify;
import com.rongke.redis.CacheUtil;
import com.rongke.service.ClassifyService;
import com.rongke.utils.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version : Ver 1.0
 * @ClassifyController
 * @二级分类Controller
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping(value = "/api/classify")
@Transactional
public class ClassifyController {
    private Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private ClassifyService classifyService;
    @Autowired
    private CacheUtil cacheUtil;

    /**
     * @param classify
     * @return 返回值JsonResp
     * @添加一、二级分类
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/addClassify", method = RequestMethod.POST)
    public JsonResp addClassify(@RequestBody Classify classify) {
        log.debug("添加一、二级分类");
        if (StringUtil.isEmpty(classify.getTitle()))
            return JsonResp.toFail("参数不能为空！");
        classifyService.insert(classify);
        return JsonResp.ok(classify);
    }

    /**
     * @param classify
     * @return 返回值JsonResp
     * @修改二级分类
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public JsonResp updateClassify(@RequestBody Classify classify) {
        log.debug("修改二级分类");
        classifyService.updateById(classify);
        return JsonResp.ok(classify);
    }

    /**
     * @param datas
     * @return
     * @删除分类
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public JsonResp deleteClassify(@RequestBody Map<String, Object> datas) {
        Long id = Long.parseLong((String) datas.get("id"));
        log.debug("删除分类");
        classifyService.deleteById(id);
        //确定要级联删除子节点数据
        if (StringUtil.isNotEmpty((String) datas.get("rec"))) {
            classifyService.deleteByClassifyType(id);
        }
        return JsonResp.ok();
    }

    /**
     * @param id
     * @return 返回值JsonResp
     * @根据id查找二级分类
     */
    @RequestMapping(value = "/selectOne", method = RequestMethod.GET)
    public JsonResp selectClassify(Long id) {
        log.debug("查找二级分类");
        Classify classify = null;
        classify = classifyService.selectById(id);
        return JsonResp.ok(classify);
    }

    /**
     * @param classifyType
     * @return
     * @根据一级分类查找二级分类列表(带一级分类图片信息)
     */
    @RequestMapping(value = "/selectClassifyListByType", method = RequestMethod.GET)
    public JsonResp selectClassifyListByType(String classifyType) {
        log.debug("根据一级分类查找二级分类列表");
        if (StringUtil.isEmpty(classifyType))
            return JsonResp.toFail("一级分类id不能为空！");
        Classify classify=classifyService.selectById(classifyType);
        List<Classify> classifyList = null;
        classifyList = classifyService.selectClassifyListByType(classifyType);
        Map<String,Object> map=new HashMap<>();
        map.put("bannerPic",classify.getPictureUrl());
        map.put("secCateList",classifyList);
        return JsonResp.ok(map);
    }

    /**
     * 获取一级分类列表
     *
     * @return
     */
    @RequestMapping(value = "/getFirstClassifyList", method = RequestMethod.GET)
    public JsonResp getFirstClassifyList() {
        log.debug("获取一级分类列表");
        List<Classify> firstClassifyList = classifyService.getFirstClassifyList();
        return JsonResp.ok(firstClassifyList);
    }

    /**
     * @return
     * @获取分类树json
     */
    @CrossOrigin(origins = "*", maxAge = 3600)
    @RequestMapping(value = "/getTree", method = RequestMethod.GET, produces = "text/plain;charset=UTF-8")
    public String getTree(int cateType) {
        log.debug("获取分类树json");
        if (cateType != 0 && cateType != 1 & cateType != 2)
            return "参数出错";
        EntityWrapper<Classify> ew = new EntityWrapper();
        ew.eq("cate_type", cateType);
        List<Classify> classifyList = classifyService.selectList(ew);
        List list = new ArrayList();
        for (Classify c : classifyList) {
            Map map1 = new HashMap();
            map1.put("id", c.getId());
//            如果父id为空，则为一级分类，即树的一级节点
            if (StringUtil.isEmpty(c.getClassifyType())) {
                map1.put("parent", "#");
            } else map1.put("parent", c.getClassifyType());
            map1.put("text", c.getTitle());
            map1.put("subtitle", c.getSubtitle());
            map1.put("picture_url", c.getPictureUrl());
            list.add(map1);
        }

        String jsonObject = JSON.toJSONString(list, true);
//        System.out.println(jsonObject.toString());
        log.debug(jsonObject);
        return jsonObject;
    }

    /**
     * @获取下一级分类列表
     * @param classifyId
     * @return
     */
    @RequestMapping(value = "/getNextClassifyList", method = RequestMethod.GET)
    public JsonResp getNextClassifyList(String classifyId) {
        log.debug("获取下一级分类列表");
        Map<String, Object> map = new HashMap<>();
        map.put("classify_type", classifyId);
        List<Classify> classifyList = classifyService.selectByMap(map);
        return JsonResp.ok(classifyList);
    }



}
